﻿using GalaSoft.MvvmLight;

using NicoView.Model.Service;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using NicoView.Model;
using GalaSoft.MvvmLight.Command;
using NicoView.ViewModel.Message;

namespace NicoView.ViewModel
{
    public sealed class NicoViewUserInfoViewModel : NicoViewCommentListViewModelBase,IDisposable
    {
        public NicoViewUserInfoViewModel()
            : this(new NotificationNicoUser(new SerializableNicoEncounterUser("")), new NicoViewCommentCollection())
        {
        }


        public NicoViewUserInfoViewModel(NotificationNicoUser userInfo,NicoViewCommentCollection comments)
        {
            _userInfo = userInfo;

            if (_userInfo != null) _userInfo.PropertyChanged += _userInfoChanged;

            
            foreach (var comment in comments)
            {
                this._addComment(comment);
            }
            
            comments.CollectionChanged += _collectionChanged;
           
            
            _basecomments = comments;

            this.openUserLink = null;
        }

        ~NicoViewUserInfoViewModel()
        {

        }

        private void _userInfoChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {

             this.RaisePropertyChanged(e.PropertyName);

        }



        private  NicoViewCommentCollection _basecomments;

        private void _collectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                
                foreach (NicoViewCommentViewModel comment in e.NewItems)
                {
                    this._addComment(comment);
                    
                }


                

                
            }
        }

        private void _addComment(NicoViewCommentViewModel comment)
        {
            bool update = false;
            if (comment.Comment.User == _userInfo)
            {
                this.Comments.Add(new NicoViewCommentViewModel(comment.Comment, this));
                update = true;
            }

            if (update) this.Comments.Upgrade();
        }

        public override bool IsCommentAccepted(NicoView.ViewModel.NicoViewCommentViewModel comment)
        {
            return base.IsCommentAccepted(comment) && comment.Comment.User.Id == this.Id;
        }


        internal override void ShowSingleUser(NicoViewShowSingleUserInfoMessage e)
        {
           
        }




        public string Id
        {
            get
            {

                return _userInfo.Id;
            }
        }

        public string NickName
        {
            get
            {

                return _userInfo.NickName;
            }
        }

        public string CustomName
        {
            get
            {

                return _userInfo.CustomName;
            }

            set
            {
                _userInfo.CustomName = value;
            }
        }

        public string UserIconPath
        {
            get
            {

                return _userInfo.UserIconPath;
            }
        }


        public ICollection<NamedColor> SelectionColors
        {
            get
            {
                return NotificationNicoUser.BackGroundColors;
            }
        }



        public NamedColor BackGroundColor
        {
            get
            {

                return this._userInfo.BackGroundColor;
            }

            set
            {
                this._userInfo.BackGroundColor = value;
            }
        }


        private ICommand openUserLink;

        public ICommand OpenUserLink
        {
            get
            {
                if (this.openUserLink == null)
                {
                    this.openUserLink = new RelayCommand(() =>
                    {
                        this.clickUserLink();
                    });

                }

                return this.openUserLink;
            }
        }

        private void clickUserLink()
        {
            NicoViewApplication.Current.ExecuteUserInfoUrl(_userInfo.Id);
        }


        private NotificationNicoUser _userInfo;

        public override void Loded()
        {

        }

        private void _settingsPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {

           this.Comments.Upgrade();
            
        }

        protected internal override void onAddComment(NicoView.Model.Service.Events.NicoCommentReceiveEventArgs args)
        {
           
        }

        protected internal override void onPeriod(NicoView.Model.Service.Events.NicoCommentReceivePeriodEventArgs args)
        {
            
        }



        public override void ConnectLive()
        {
            
        }

        public new void Dispose()
        {
            if (_userInfo != null) _userInfo.PropertyChanged -= _userInfoChanged;
            _basecomments.CollectionChanged -= _collectionChanged;


            _basecomments = null;
            base.Dispose();
        }
    }
}
